package com.yanqu.road.dao.impl.cross.unionwar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionwar.UserCricketSendTimes;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/11/5 17:56
 */
public class UnionWarUserCricketSendTimesDaoImpl extends TempDataObjectDao<UserCricketSendTimes> {

    public static String getSql = "select * from t_u_activity_unionwar_user_cricket where activityId=?";
    public Map<Long, Map<Integer, UserCricketSendTimes>> getUserCricketSendTimes(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Integer, UserCricketSendTimes>> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCricketSendTimes data = parse(rs);
                if(!resultMap.containsKey(data.getUserId())){
                    resultMap.put(data.getUserId(), new ConcurrentHashMap<>());
                }
                resultMap.get(data.getUserId()).put(data.getCricketId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }
    public static String insertSql = "insert into t_u_activity_unionwar_user_cricket(activityId,userId,cricketId,times) values (?,?,?,?)";

    @Override
    protected boolean addData(UserCricketSendTimes data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCricketId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimes()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "update t_u_activity_unionwar_user_cricket set times=? where activityId=? and userId=? and cricketId=?";

    @Override
    protected boolean updateData(UserCricketSendTimes data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCricketId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserCricketSendTimes parseData(ResultSet rs) throws SQLException {
        UserCricketSendTimes data = new UserCricketSendTimes();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setCricketId(rs.getInt("cricketId"));
        data.setTimes(rs.getInt("times"));

        return data;
    }
}
